#include "TextDB.h"
#include "Utils.h"

#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <vector>

#include <iostream>

unsigned int category_id = 0;

TextDB::TextDB(const char* filename, bool create)
{
  this->filename = filename;
  if(create)
  {
	 filedb.open(filename, std::ios_base::out);
	 filedb << Utils::GetTime() << ". Baza danych została pomyślnie utworzona.\n";
  }
  else
	 filedb.open(filename, std::ios_base::in);
  
  filedb.close();
}

TextDB::~TextDB(void)
{
  if(filedb.is_open())
	 filedb.close();
}

bool TextDB::CategoryExist(const char* category)
{
  std::map<std::string, unsigned int>::const_iterator it = categories.find(category);
  return it != categories.end();
}

void TextDB::Disconnect(void)
{
  if(filedb.is_open())
	 filedb.close();
}

const char* TextDB::GetCreationDate(void)
{
  filedb.open(filename, std::ios_base::in);
  
  std::string line;
  std::getline(filedb, line);
  
  std::vector<std::string> creation_date;
  boost::split(creation_date, line, boost::is_any_of("."));
  
  filedb.close();
  
  return creation_date[0].c_str();
}

const char* TextDB::GetDataBase(bool from_file)
{
  std::string line, textdb;
  
  // Read data from file.
  if(from_file)
  {
	 filedb.open(filename, std::ios_base::in);
	 
	 while(getline(filedb, line))
		textdb += line;
	 
	 filedb.close();
  }
  // Read data from products container.
  else
  {
	 std::string category = "", date = "";
	 for(std::multimap<std::string, Product>::iterator it = products.begin(); it != products.end(); ++it)
	 {
		if(boost::lexical_cast<std::string>((*it).first) != category)
		{
		  category = (*it).first;
		  textdb += "\n---------------\n"+category+"\n---------------";
		  date = "";
		}
		Product p = (*it).second;
		if(boost::lexical_cast<std::string>(p.GetDate()) != date)
		{
		  date = p.GetDate();
		  textdb += "\n"+date+" ";
		}
		textdb += boost::lexical_cast<std::string>(p.GetName())+"-"+boost::lexical_cast<std::string>(p.GetPrice())+";";
	 }
  }
  
  return textdb.c_str();
}

void TextDB::AddProduct(const char* date, const char* category, const char* all_products)
{
  if(!CategoryExist(category))
	 categories[category] = ++category_id;
  
  std::vector<std::string> products_list;
  boost::split(products_list, all_products, boost::is_any_of(";"));
  
  for(std::vector<std::string>::iterator it = products_list.begin(); it != products_list.end(); ++it)
  {
	 if(*it != "")
	 {
		std::vector<std::string> product_price;
		boost::split(product_price, *it, boost::is_any_of("-"));
		const char* product = product_price[0].c_str();
		float price = atof(product_price[1].c_str());
		std::cout << product << " " << price << " " << date << std::endl;
		products.insert(std::pair<std::string, Product>(category, Product(categories[category], date, product, price)));
	 }
  }
  
  /*for(std::multimap<std::string, Product>::iterator it = products.begin(); it != products.end(); ++it)
  {
	 std::cout << "CATEGORY: " << (*it).first << " ";
	 Product p = (*it).second;
	 std::cout << "PRODUCT: " << p.GetName() << " " << p.GetPrice() << "; ";
	 std::cout << std::endl;
  }*/
}